import math

info = []


def add_info():
    """添加学员"""
    student_id = input("请输入学号： ")
    student_name = input("请输入姓名： ")
    student_tel = input("请输入电话号码： ")

    global info

    for i in info:
        if student_name == i['name']:
            print('用户已存在')
            break
    # 如果学员不存在，创建一个新字典并把学员信息添加到字典
    dict1 = {'name': student_name, 'id': student_id, 'tel': student_tel}
    # 将字典里的信息加入到列表里
    info.append(dict1)
    print(info)


# add_info()

def del_info():
    """删除学员"""
    while True:
        input_id = int(input("请输入要删除的学号： "))

        global info

        if 0 <= input_id <= len(info):  # 检查学员是否存在，存在则删除列表指定下标的数据
            flag_tip = input("是否删除，请输入yes or no:")
            if flag_tip == "yes":
                del info[input_id]
            print(info)
            # 删除了目标学员信息后退出循环
            break
        else:
            print("输入学员学号有误，请重新输入")


# add_info()
# del_info()


def modify_info():
    """修改学员信息"""
    while True:
        input_id = int(input("请输入要修改的学号："))

        global info

        if 0 <= input_id <= len(info):
            print(
                f'该学员的学号是{info[input_id]["id"]},姓名是{info[input_id]["name"]},电话号码是{info[input_id]["tel"]}')
            info[input_id]["id"] = int(input('输入新的学号：'))
            info[input_id]["name"] = input('输入新的姓名：')
            info[input_id]["tel"] = input('输入新的电话号码：')
            print(info)
            break
        else:
            print('输入的学号有误，请重新输入')


# add_info()
# modify_info()


def search_info():
    """查询信息"""
    input_name = input("请输入要查询的名字：")

    global info

    for i in info:
        if input_name == i['name']:
            print('***查询到的信息如下***')
            print(f'该学员的姓名是{i["name"]},学号是{i["id"]},电话号码是{i["tel"]}')
            break
        else:
            print("查无此人")


# add_info()
# search_info()


def print_all():
    for i in info:
        print(f'学号：{i["id"]},姓名：{i["name"]},电话号码：{i["tel"]}')


# add_info()
# add_info()
# print_all()


# 递归

# def sum_numbers(num):     # 3+2+1
#     if num == 1:
#         return 1   # 出口值
#     result = num + sum_numbers(num - 1)
#     return result
#
#
# sum_result = sum_numbers(3)
# print(sum_result)


# 小虎读书，第一天读了全书的一半加两页，第二天读了剩下的一半加两页......第六天读完了最后三页，问全书有多少页
#
# def whole_book(n):
#     if n > 6:
#         return 0
#     elif n == 6:
#         return 3
#     else:
#         return (whole_book(n + 1) + 2) * 2
#
#
# print("全书共：{}页".format(whole_book(1)))
# sum1 = whole_book(1)
# for i in range(1, 7):
#     print("第{}天读了{}页，剩余{}页".format(i, sum1 - whole_book(i + 1), whole_book(i + 1)))
#     sum1 = whole_book(i + 1)


# f1 = lambda: 100
# print(f1)
# print(f1())
# a = 1000
# b = 500
# print(lambda a, b: a if a > b else b)

# map(func,list)  将函数作用到列表中，返回列表
# reduce  filter(func,list) 过滤掉不符合条件的元素，返回第一个filter对象，如果要转换为列表，可以用list()来转换

# 面向对象

# class Washer:
#     def __init__(self, width, height):     # 初始化对象
#         self.width = width
#         self.height = height

#
# def print_info(self):
#     print(f'洗衣机的宽度是{self.width}')
#     print(f'洗衣机的高度是{self.height}')
#

# def __str__(self):
#     return '这是海尔洗衣机的说明书'
#
# def __del__(self):
#     print(f'{self}对象已删除')


# haier1 = Washer(10, 20)
# haier1.print_info()
# print(haier1)
# del haier1


# 烤地瓜

# class SweetPotato:
#
#     def __init__(self):
#         self.cook_time = 0
#         self.cook_static = '生的'
#         self.condiments = []
#
#     def cook(self, time):
#         self.cook_time += time
#         if 0 <= self.cook_time < 3:
#             self.cook_static = '生的'
#         elif 3 <= self.cook_time < 5:
#             self.cook_static = '半生不熟'
#         elif 5 <= self.cook_time < 8:
#             self.cook_static = '熟了'
#         elif 8 <= self.cook_time:
#             self.cook_static = '烤糊了'
#
#     def add_condiments(self, condiments):
#         self.condiments.append(condiments)
#
#     def __str__(self):
#         return f'这个地瓜烤了{self.cook_time}分钟，状态是{self.cook_static}'


# digua1 = SweetPotato()
# digua1.cook(2)
# digua1.add_condiments('蜂蜜')
# print(digua1)
# digua1.cook(3)
# digua1.add_condiments('饼干碎')
# print(digua1)
# digua1.cook(2)
# print(digua1)
# digua1.cook(2)
# print(digua1)

#
# class Furniture:
#     def __init__(self, name, area):
#         self.name = name
#         self.area = area
#
#
# class House:
#     def __init__(self, address, area):
#         self.address = address
#         self.area = area
#         self.free_area = area
#         self.furniture = []
#
#     def __str__(self):
#         return f'房子坐落在{self.address},占地面积{self.area},剩余面积{self.free_area},家具有{self.furniture}'
#
#     def add_furniture(self, item):
#         if self.free_area >= item.area:
#             self.furniture.append(item.name)
#             self.free_area -= item.area
#         else:
#             print('家具太大，剩余面积不足，无法容纳')


# bed = Furniture('双人床', 6)
# jia1 = House('北京', 1200)
# print(jia1)

# jia1.add_furniture(bed)
# print(jia1)
#
# sofa = Furniture('沙发', 10)
# jia1.add_furniture(sofa)
# print(jia1)
#
# ball = Furniture('篮球场', 1500)
# jia1.add_furniture(ball)
# print(jia1)

#
# class Person:
#     def __init__(self, name, weight):
#         self.name = name
#         self.weight = weight
#
#     def __str__(self):
#         return f'名字是{self.name},体重是{self.weight}'
#
#     def PaoBu(self):
#         self.weight -= 0.5
#         print(f'本次跑步后的体重是{self.weight}')
#
#     def Eating(self):
#         self.weight += 1
#         print(f'这次吃东西后的体重是{self.weight}')


# Person1 = Person('XiaoMing', 75)
# Person1.PaoBu()
# Person1.PaoBu()
# print(Person1)
#
# Person2 = Person('XiaoHong', 45)
# Person2.Eating()
# Person2.PaoBu()
# print(Person2)

# 多继承
# class Master:
#     def __init__(self):
#         self.kongfu = '[古法]'
#
#     def make_cake(self):
#         print(f'用{self.kongfu}做的')
#
#
# class School:
#     def __init__(self):
#         self.kongfu = '[新式]'
#
#     def make_cake(self):
#         print(f'用{self.kongfu}做的')
#
#
# class Practicer(School, Master):
#     def __init__(self):
#         super().__init__()
#         self.kongfu = '[独创]'
#
#     def make_cake(self):
#         # 如果先调用了父类的属性和方法，父类属性回覆盖子类属性，故在调用属性前，先调用自己子类的初始化
#         self.__init__()
#         print(f'用{self.kongfu}做的')
#
#     # 调用父类方法，但是为保证调用到的也是父类的属性，必须在调用方法前调用父类的初始化
#     def make_Master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#
#     def make_School_cake(self):
#         School.__init__(self)
#         School.make_cake(self)


# XiaoHong = Practicer()
# XiaoHong.make_cake()
# XiaoHong.make_Master_cake()
# XiaoHong.make_School_cake()
# XiaoHong.make_cake()


class Master:
    def __init__(self):
        self.kongfu = '[古法]'

    def make_cake(self):
        print(f'用{self.kongfu}做的')


class School(Master):
    def __init__(self):
        super().__init__()
        self.kongfu = '[新式]'

    def make_cake(self):
        print(f'用{self.kongfu}做的')

        super().__init__()  # super()继承 调用父类方法
        super().make_cake()


class Practice(School):
    def __init__(self):
        super().__init__()
        self.kongfu = '[独创]'

    def make_cake(self):
        # 如果先调用了父类的属性和方法，父类属性回覆盖子类属性，故在调用属性前，先调用自己子类的初始化
        self.__init__()
        print(f'用{self.kongfu}做的')

    # 调用父类方法，但是为保证调用到的也是父类的属性，必须在调用方法前调用父类的初始化
    def make_Master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_School_cake(self):
        School.__init__(self)
        School.make_cake(self)

    def make_old_cake(self):
        super().__init__()
        super().make_cake()


xiao = Practice()
xiao.make_old_cake()


# 定义一个三角形 求三角形的面积和周长
# class Triangle(object):
#     def __init__(self, a, b, c):
#         if not (a + b > c and a + c > b and b + c > a):
#             raise ValueError('给定的边长不能构成一个三角形')
#         self.a = a
#         self.b = b
#         self.c = c
#
#     def triangle_area(self):
#         p = (self.a + self.b + self.c) / 2
#         s1 = p * (p - self.a) * (p - self.b) * (p - self.c)
#         s = math.sqrt(s1)
#         return s
#
#     def triangle_cal(self):
#         return self.a + self.b + self.c
#
#
# class Test(Triangle):
#     pass
#
#
# t = Test(3, 4, 5)
# print(f'该三角形的面积是{t.triangle_area()}')
# print(f'该三角形的周长是{t.triangle_cal()}')



